package com.chookss.home.inspection;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.entity.InspectionPicEntity;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.google.gson.reflect.TypeToken;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class InspectionGalleryListActivity extends BaseAct {


    @BindView(R.id.common_title_txt)
    TextView common_title_txt;
    @BindView(R.id.rvListView)
    RecyclerView rvListView;
    @BindView(R.id.rvView)
    RecyclerView recyclerView;
    @BindView(R.id.srlView)
    SmartRefreshLayout smrRf;
    @BindView(R.id.llBg)
    LinearLayout llBg;
    private int currentPage = 1;
    private boolean isEnd = false;//是否到底
    private InspectionListAdapter listAdapter;
    private List<InspectionPicEntity> galleryListEntityList = new ArrayList<>();
    private InspectionAdapter adapter;
    private List<InspectionPicEntity.ImagePathListBean> imagePathListBeanList = new ArrayList<>();
    private View emptyListView;
    private TextView tvListEmpty;//空布局;
    private String createTime="",groupCode="";//是否查询某一天具体的巡检

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        setContentView(R.layout.activity_inspection_gallery_list);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        createTime=getIntent().getStringExtra("createTime");
        groupCode=getIntent().getStringExtra("groupCode");
        if(Utils.isNull(createTime)){
            //列表
            common_title_txt.setText("巡检记录");
            rvListView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            listAdapter = new InspectionListAdapter(R.layout.item_gallery_list, this, galleryListEntityList);
            rvListView.setAdapter(listAdapter);
            ((SimpleItemAnimator) rvListView.getItemAnimator()).setSupportsChangeAnimations(false);
            rvListView.setItemAnimator(null);
            emptyListView = LayoutInflater.from(this).inflate(R.layout.layout_common_empty, rvListView, false);
            rvListView.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
        }else{
            //当日
            common_title_txt.setText(createTime);
            GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 4);
            recyclerView.setLayoutManager(gridLayoutManager1);
            GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration(4, Utils.dip2px(this, 2), false);
            recyclerView.addItemDecoration(gridSpacingItemDecoration);
            adapter = new InspectionAdapter(R.layout.item_gallery, this, imagePathListBeanList,1);
            recyclerView.setAdapter(adapter);
            ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
            emptyListView = LayoutInflater.from(this).inflate(R.layout.layout_common_empty, recyclerView, false);
            rvListView.setVisibility(View.GONE);
            recyclerView.setVisibility(View.VISIBLE);
        }
        tvListEmpty = emptyListView.findViewById(R.id.tvEmpty);
        tvListEmpty.setText("暂无数据");
        smrRf.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (isEnd) {//has next page
                    MyToast.show("没有更多数据了");
                    loadEnd();
                } else {
                    currentPage++;
                    if(Utils.isNull(createTime)){
                        getListData();
                    }else{
                        getData();
                    }
                }
            }
        });
        smrRf.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                currentPage = 1;
                isEnd = false;
                if(Utils.isNull(createTime)){
                    getListData();
                }else{
                    getData();
                }
            }
        });
        smrRf.autoRefresh();
    }
    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNum", currentPage + "");
        map.put("pageSize", "20");
        map.put("groupCode", groupCode + "");
        listAdapter.setGroupCode(groupCode);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<InspectionPicEntity>>>() {
        }.getType(), null, Urls.selectAppXunjianImageList, SignUtils2.makeSignStr(map), false, new ObserverCallback<List<InspectionPicEntity>>() {
            @Override
            public void onSuccess(List<InspectionPicEntity> data) {
                if (currentPage == 1) {
                    galleryListEntityList.clear();
                    listAdapter.notifyDataSetChanged();
                }
                if (data != null && data.size() > 0) {
                    galleryListEntityList.addAll(data);
                    listAdapter.notifyDataSetChanged();
                } else {
                    isEnd = true;
                    if (galleryListEntityList.size() == 0) {
                        listAdapter.setEmptyView(emptyListView);
                    }
                }
                loadEnd();
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    private void getData() {
        Map<String, String> allmap = new HashMap<>();
        allmap.put("createTime", createTime);
        allmap.put("pageNum", currentPage + "");
        allmap.put("groupCode", groupCode + "");
        allmap.put("pageSize", "40");
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<List<InspectionPicEntity.ImagePathListBean>>>() {
        }.getType(), null, Urls.selectAppXunjianImageListByTime, SignUtils2.makeSignStr(allmap), false, new ObserverCallback<List<InspectionPicEntity.ImagePathListBean>>() {
            @Override
            public void onSuccess(List<InspectionPicEntity.ImagePathListBean> data) {
                if (currentPage == 1) {
                    imagePathListBeanList.clear();
                    adapter.notifyDataSetChanged();
                }
                if (smrRf == null) {
                    return;
                }
                int oldIndex = imagePathListBeanList.size();
                if (data != null && data.size() > 0) {
                    imagePathListBeanList.addAll(data);
                    if (currentPage == 1) {
                        adapter.notifyDataSetChanged();
                    } else {
                        adapter.notifyItemRangeChanged(oldIndex, data.size());
                    }
                } else {
                    isEnd = true;
                    if (imagePathListBeanList.size() == 0) {
                        listAdapter.setEmptyView(emptyListView);
                    }

                }
                loadEnd();
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    @OnClick({R.id.common_title_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
        }
    }
    private void loadEnd() {
        if (null != smrRf && smrRf.getState() == RefreshState.Refreshing)
            smrRf.finishRefresh(true);
        if (null != smrRf && smrRf.getState() == RefreshState.Loading)
            smrRf.finishLoadMore(true);
    }

}
